import {Injectable} from "@angular/core";
import {ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot} from "@angular/router";
import {AppConstants, Components} from "../constants/constants";
import {DataStorageUtil} from "../util/data-storage-util";

@Injectable()
export class DispatcherResolveService implements Resolve<string>{

  constructor(
    private router : Router,
    private dataStorage: DataStorageUtil
  ) { }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<string> | string{
        let isLogin = this.dataStorage.get(AppConstants.isLogin);
        let home = this.dataStorage.get(AppConstants.home);
        if (!!isLogin && AppConstants.loginSucess == isLogin) {
          return home;
        }else{
          this.router.navigate([Components.authenComponent]);
          return null;
        }
  }
}
